package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostActivity;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = CrossProtocol.C2_GHOST_ACTIVITY_ATTACK_BOSS, desc = "鬼市跨服打boss")
public class CrossGhostAttackBossCmd extends CrossCommand {

    int activityId;
    long serverId;
    int patronsId;
    String cost;
    int bossId;
    long ability;
    int buffAddition;
    int talentAddition;
    int attackTimes;
    PlayerProto.PlayerBaseTempMsg playerBaseTempMsg;
    long userId;
    String unionUid = "";
    long userResetTime;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.CrossAttackBossReqMsg reqMsg = GhostActivityProto.CrossAttackBossReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        serverId = reqMsg.getServerId();
        patronsId = reqMsg.getPatronsId();
        cost = reqMsg.getCost();
        bossId = reqMsg.getBossId();
        ability = reqMsg.getAbility();
        buffAddition = reqMsg.getBuffAddition();
        talentAddition = reqMsg.getTalentAddition();
        attackTimes = reqMsg.getAttackTimes();
        playerBaseTempMsg = reqMsg.getPlayerInfo();
        userId = packet.getPlayerId();
        if (reqMsg.hasUnionUid()) {
            unionUid = reqMsg.getUnionUid();
        }
        userResetTime = reqMsg.getUserResetTime();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        GhostActivity activity = GhostMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        ret = activity.attackBoss(serverId, patronsId, cost, bossId, ability,
                buffAddition, talentAddition, attackTimes, PlayerBasePb.parseBaseUserInfo(playerBaseTempMsg), userId,
                unionUid, userResetTime);
    }

    @Override
    public void sendData() {
        if (ret != 0 && ret != GameErrorCode.E_GHOST_ACTIVITY_BOSS_DEAD) {
            GhostActivityProto.CrossAttackBossRespMsg.Builder builder = GhostActivityProto.CrossAttackBossRespMsg.newBuilder();
            builder.setRet(ret);
            builder.setPatronsId(patronsId);
            builder.setCost(cost);
            builder.setAttackTimes(attackTimes);
            builder.setUserResetTime(userResetTime);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_GHOST_ATTACK_BOSS_FROM_CROSS, builder));
        }
    }
}
